package com.example.bookstore;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class BookDetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_detail);
        //获取到book对象
        Book book = getIntent().getParcelableExtra("book");

        //Toast.makeText(this, "出版社" + book.getBookPress(), Toast.LENGTH_SHORT).show();
        //分别获得view对象
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        MyImageView myImageView = (MyImageView) findViewById(R.id.bool_image);
        TextView bookName = (TextView) findViewById(R.id.book_name);
        TextView bookTitle = (TextView) findViewById(R.id.book_title);
        TextView bookInformation = (TextView) findViewById(R.id.book_information);
        TextView bookAuthor = (TextView) findViewById(R.id.book_author);
        TextView bookPress = (TextView) findViewById(R.id.book_press);
        TextView bookPressData = (TextView) findViewById(R.id.book_pressData);
        TextView bookPrice = (TextView)findViewById(R.id.book_price);

        myImageView.setImageURL(book.getBookImage());
        bookName.setText(book.getBookName());
        bookAuthor.setText(book.getBookAuthor());
        bookTitle.setText(book.getBookTitle());
        bookPress.setText(book.getBookPress());
        bookPressData.setText(book.getPressDate());
        bookInformation.setText(book.getBookDescribe());
        bookPrice.setText(book.getBookPrice());
        //监听立即购买和加入购物车
        LinearLayout addShoppingCar = (LinearLayout) findViewById(R.id.add_shopping_car);
        LinearLayout buy_now = (LinearLayout) findViewById(R.id.buy_now);

        buy_now.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //检测立即购买
                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                builder.setMessage("确定要购买吗");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        buyNow(Content.user.getAccount(), Integer.toString(book.getId()),WebConnect.randomStatus());
                    }
                });
                builder.setNegativeButton("取消" ,null);
                builder.show();
            }
        });


        addShoppingCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置加入购物车监听事件
                updateShoppingCar(Content.user.getAccount(), Integer.toString(book.getId()));
                Toast.makeText(v.getContext(), "已添加购物车", Toast.LENGTH_SHORT).show();
            }
        });
    }


    private void updateShoppingCar(String account, String bookId) {
        new Thread(() -> {
            try {
                OkHttpClient client = new OkHttpClient();
                RequestBody requestBody = new FormBody.Builder()
                        .add("account", account)
                        .add("newBookId", bookId)
                        .build();
                Request request = new Request.Builder()
                        .url("http://www.fenchingking.top/BookStore/updateShoppingCar")
                        .post(requestBody)
                        .build();
                Response response = client.newCall(request).execute();
                String responseData = response.body().string();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }

    private void buyNow(String account, String bookId,String status) {
        new Thread(() -> {
            try {
                OkHttpClient client = new OkHttpClient();
                RequestBody requestBody = new FormBody.Builder()
                        .add("userAccount", account)
                        .add("bookList", bookId)
                        .add("status", status)
                        .add("position","buyNow")
                        .build();
                Request request = new Request.Builder()
                        .url("http://www.fenchingking.top/BookStore/createOrder")
                        .post(requestBody)
                        .build();
                Response response = client.newCall(request).execute();
                String responseData = response.body().string();
                change(responseData);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }
    private void change(final String str){
        runOnUiThread(() ->{
                    try {
                         Double price = Double.parseDouble(str);

                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show();
                    }
                }
        );
    }
}